package com.rent.apartment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import java.util.stream.Collectors;

import com.rent.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rent.apartment.mapper.ApartmentInfoMapper;
import com.rent.apartment.domain.ApartmentInfo;
import com.rent.apartment.service.IApartmentInfoService;

/**
 * 公寓信息Service业务层处理
 *
 * @author cooper
 * @date 2024-10-09
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo> implements IApartmentInfoService {
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    /**
     * 查询公寓信息
     *
     * @param id 公寓信息主键
     * @return 公寓信息
     */
    @Override
    public ApartmentInfo selectApartmentInfoById(Long id) {
        return apartmentInfoMapper.selectApartmentInfoById(id);
    }

    /**
     * 查询公寓信息列表
     *
     * @param apartmentInfo 公寓信息
     * @return 公寓信息
     */
    @Override
    public List<ApartmentInfo> selectApartmentInfoList(ApartmentInfo apartmentInfo) {
        return apartmentInfoMapper.selectApartmentInfoList(apartmentInfo);
    }

    /**
     * 新增公寓信息
     *
     * @param apartmentInfo 公寓信息
     * @return 结果
     */
    @Override
    public int insertApartmentInfo(ApartmentInfo apartmentInfo) {
        apartmentInfo.setCreateTime(DateUtils.getNowDate());
        return apartmentInfoMapper.insertApartmentInfo(apartmentInfo);
    }

    /**
     * 修改公寓信息
     *
     * @param apartmentInfo 公寓信息
     * @return 结果
     */
    @Override
    public int updateApartmentInfo(ApartmentInfo apartmentInfo) {
        apartmentInfo.setUpdateTime(DateUtils.getNowDate());
        return apartmentInfoMapper.updateApartmentInfo(apartmentInfo);
    }

    /**
     * 批量删除公寓信息
     *
     * @param ids 需要删除的公寓信息主键
     * @return 结果
     */
    @Override
    public int deleteApartmentInfoByIds(Long[] ids) {
        return apartmentInfoMapper.deleteApartmentInfoByIds(ids);
    }

    /**
     * 删除公寓信息信息
     *
     * @param id 公寓信息主键
     * @return 结果
     */
    @Override
    public int deleteApartmentInfoById(Long id) {
        return apartmentInfoMapper.deleteApartmentInfoById(id);
    }

    /**
     * 根据城市获取社区列表
     *
     * @param city 城市名称
     * @return 社区列表
     */
    @Override
    public List<String> selectApartmentInfoByCity(String city) {
        if (city != null && !city.trim().isEmpty()) {
            ApartmentInfo apartmentInfo = new ApartmentInfo();
            apartmentInfo.setAddressCity(city);
            return apartmentInfoMapper.selectApartmentInfoList(apartmentInfo)
                    .stream()
                    .map(ApartmentInfo::getAddressNeighborhood)
                    .distinct()
                    .collect(Collectors.toList());
        } else {
            // 处理 city 为空的情况
            throw new IllegalArgumentException("City cannot be null or empty.");
        }

    }

    /**
     * 根据城市和社区获取小区列表
     *
     * @param city 城市名称
     * @param neighborhood 社区名称
     * @return 小区列表
     */
    @Override
    public List<String> selectCommunitiesByCityAndNeighborhood(String city, String neighborhood) {
        if (city != null && !city.trim().isEmpty() && neighborhood != null && !neighborhood.trim().isEmpty()) {
            ApartmentInfo apartmentInfo = new ApartmentInfo();
            apartmentInfo.setAddressCity(city);
            apartmentInfo.setAddressNeighborhood(neighborhood);
            return apartmentInfoMapper.selectApartmentInfoList(apartmentInfo)
                    .stream()
                    .map(ApartmentInfo::getAddressCommunity)
                    .distinct()
                    .collect(Collectors.toList());
        } else {
            throw new IllegalArgumentException("City and neighborhood cannot be null or empty.");
        }
    }
}